<!--
 * @Description: 首页
 * @Author: zhihong deng
 * @Date: 2021-08-17 21:38:38
 * @LastEditors: zhihong deng
 * @LastEditTime: 2021-10-11 23:05:54
-->
<template>
  <div class="basic-layout">
    <div :class="['nav-size',isCollapse ? 'fold':'unfold']">
      <!-- 系统LOGO -->
      <div class="log">
        <img src="../assets/logo.png" alt="">
        <span>Manager</span>
      </div>
      <!-- 导航菜单 -->
      <el-menu
        default-active="1"
        class="nav-menu"
        router 
        :collapse="isCollapse"
        background-color="#001529"
        text-color="#fff"
      >
        <tree-menu :userMenu="userMenu" />
      </el-menu>
    </div>
    <div :class="['content-right',isCollapse ? 'fold':'unfold']">
      <div class="nav-top">
        <div class="nav-left">
          <div class="menu-fold" @click="toggle">
            <i class="el-icon-s-fold"></i>
          </div>
          <div class="bread">
            <bread-crumb></bread-crumb>
          </div>
        </div>
        <div class="user-info">
          <el-badge
            :is-dot="noticeCount > 0 ? true : false"
            class="notice"
            type="danger"
            @click="$router.push('/audit/approve')"
          >
            <i class="el-icon-bell"></i>
          </el-badge>
          <el-dropdown @command="handleLogout">
            <span class="user-link">
              {{userInfo.userName}}
              <i class="el-icon--right"></i>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="email">
                  邮箱：{{userInfo.userEmail}}
                  </el-dropdown-item>
                <el-dropdown-item command="logout">退出</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
          </div>
      </div>
      <div class="wrapper">
        <router-view></router-view>
      </div>
    </div>
  </div>
</template>

<script>
import TreeMenu from './TreeMenu.vue'
import BreadCrumb from './BreadCrumb.vue'
export default {
  nama: 'Home',
  components:{
    TreeMenu,
    BreadCrumb
  },
  data() {
    return {
      isCollapse: false,
      userInfo:this.$store.state.userInfo,
      noticeCount: 0,
      userMenu: []
    }
  },
  computed: {
    noticeCount() {
      return this.$store.state.noticeCount;
    }
  },
  mounted() {
    this.getNoticeCount()
    this.getMenuList()
    // this.getUserList()
  },
  methods: {
    // 收缩展开
    toggle(){
      this.isCollapse = !this.isCollapse
    },
    // 退出
    handleLogout(key){
      if(key == 'email') return;
      this.$store.commit('saveUserInfo','');
      this.userInfo = {};
      this.$router.push('/login');
    },
    // 未读消息数量
    async getNoticeCount() {
      try{
        const count = await this.$api.noticeCount()
        this.$store.commit("saveNoticeCount", count)
      }catch(error){
        console.error(error);
      }
    },
    // 获取菜单列表
    async getMenuList() {
      try{
        const { menuList, actionList } = await this.$api.getPermissionList()
        this.userMenu = menuList
        this.$store.commit("saveUserMenu", menuList)
        this.$store.commit("saveUserAction", actionList)
      }catch(error){
        console.error(error);
      }
    },
    // 获取用户列表
    // async getUserList() {
    //   try{
    //     const { list, page } = await this.$api.getUserList();
    //     console.log('获取用户列表',list, page);
    //   }catch(error){
    //     console.error(error);
    //   }
    // }
  },
}
</script>

<style lang="scss">
  .basic-layout{
    position: relative;
    .nav-size{
      position: fixed;
      width: 200px;
      height: 100vh;
      background: #001529;
      color: #fff;
      overflow: auto;
      transition: width .5s;
      .log{
        display: flex;
        align-items: center;
        font-size: 18px;
        height: 50px;
        img{
          margin: 0 16px;
          width: 32px;
          height: 32px;
        }
      }
      .nav-menu{
        height: calc(100vh - 50px);
        border-right: none;
      }
      // 合并
      &.fold{
        width: 64px;
      }
      // 展开
      &.unfold{
        width: 200px;
      }
    }
    .content-right{
      margin-left: 200px;
      .nav-top{
        height: 50px;
        line-height: 50px;
        display: flex;
        justify-content: space-between;
        border-bottom: 1px solid #ddd;
        padding: 0 20px;
        .nav-left{
          display: flex;
          align-items: center;
          .menu-fold{
            margin-right: 15px;
          }
        }
        .user-info{
          .notice{
            line-height: 30px;
            margin-right: 15px;
            cursor: pointer;
          }
          .user-link{
            cursor: pointer;
            color: #409ef4;
          }
        }
      }
      .wrapper{
        background: #eee;
        padding: 20px;
        height: calc(100vh - 50px);
        .main-page{
          background: #fff;
          height: 100%;
        }
      }
      &.fold{
        margin-left: 64px;
      }
      &.unfold{
        margin-left: 200px;
      }
    }
  }
</style>